package net.sashag.nadlandroid;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.util.Log;

import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

public class Message {
	
	private String apartmentId;
	private Date sendTime;
	private String senderName;
	private String senderEmail;
	private String senderPhone;
	private String messageId;
	
	private Apartment apartment; //Not populated until fetch call
	
	public Message(String senderName, String senderEmail, String senderPhone, String apartmentId, Date sendTime, String messageId) {
		this.senderName = senderName;
		this.senderEmail = senderEmail;
		this.senderPhone = senderPhone;
		this.apartmentId = apartmentId;
		this.sendTime = sendTime;
		this.messageId = messageId;
	}
	
	private Message(ParseObject messageObject) {
		this(messageObject.getString("SenderName"),
				messageObject.getString("SenderEmail"),
				messageObject.getString("SenderPhone"),
				messageObject.getString("ApartmentId"),
				messageObject.getDate("SendTime"),
				messageObject.getObjectId());
	}
	
	public Apartment getApartment() {
		return apartment;
	}
	public Date getSendTime() {
		return sendTime;
	}
	public String getSenderName() {
		return senderName;
	}
	public String getSenderEmail() {
		return senderEmail;
	}
	public String getSenderPhone() {
		return senderPhone;
	}
	
	public void fetchApartmentInfo() throws ParseException {
		apartment = Apartment.fromId(apartmentId);
	}
	
	public void saveAsNewParseObject() {
		ParseObject messageObject = new ParseObject("Message");
		messageObject.put("SenderName", senderName);
		messageObject.put("SenderEmail", senderEmail);
		messageObject.put("SenderPhone", senderPhone);
		messageObject.put("ApartmentId", apartmentId);
		messageObject.put("SendTime", sendTime);
		messageObject.put("ReceiverId", ParseUser.getCurrentUser().getObjectId());
		messageObject.saveEventually();
	}
	
	public static List<Message> getMessages(Date newerThan, boolean withApartmentInfo) throws ParseException {
		ParseQuery query = new ParseQuery("Message");
		query.whereEqualTo("ReceiverId", ParseUser.getCurrentUser().getObjectId());
		if (newerThan != null) {
			query.whereGreaterThan("createdAt", newerThan);
		}
		query.addDescendingOrder("createdAt");
		List<ParseObject> messageObjects = query.find();
		List<Message> messages = new ArrayList<Message>();
		for (int i = 0; i < messageObjects.size(); ++i) {
			Message message = new Message(messageObjects.get(i));
			try {
				message.fetchApartmentInfo();
			} catch (ParseException e) {
				//We don't want to abort the entire loading process because of one message
				//that is not up to date. Ignore this error (the apartment may have been removed).
				Log.w("Message", "Error fetching apartment for message: " + e.getMessage());
				continue;
			}
			messages.add(message);
		}
		return messages;
	}

	public void delete() {
		ParseQuery query = new ParseQuery("Message");
		query.getInBackground(messageId, new GetCallback() {
			public void done(ParseObject message, ParseException e) {
				if (e != null) {
					Log.w("Message", "Error retrieving message for deletion: " + messageId);
				} else {
					message.deleteInBackground();
				}
			}
		});
	}
	
}
